package com.chaoyi.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.chaoyi.model.BarrageAndUser;
import com.chaoyi.service.BarrageAndUserService;
import com.chaoyi.util.upload.FileType;
import com.chaoyi.util.upload.UploadRes;

@Controller
@MultipartConfig
@RequestMapping("barrageAndUser")
public class BarrageAndUserController {
	@Resource
	BarrageAndUserService barrageAndUserService;
	/**
	 * 初始化跳转页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request ,HttpServletResponse response){
		ModelAndView mod = new ModelAndView("/barrageAndUser/barrageAndUserList");
		return mod;
	}
	
	/**
	 * 弹幕和在线用户展示
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("barrageAndUserList")
	@ResponseBody
	public JSONObject barrageAndUserList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		String offset = request.getParameter("offset");
		String limit = request.getParameter("limit");
		List<BarrageAndUser> list = barrageAndUserService.findList(offset, limit);
		int count = barrageAndUserService.findCount();
		json.put("rows", list);
		json.put("total", count);
		return json;
	}
	
	/**
	 *  新增弹幕和在线用户跳转页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("addBarrageAndUser_jump")
	public ModelAndView addBarrageAndUser_jump(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mod = new ModelAndView("/barrageAndUser/addBarrageAndUser");
		return mod;
	}
	/**
	 * 保存新增弹幕和在线用户内容
	 */
	@ResponseBody
	@RequestMapping("addBarrageAndUser")
	public JSONObject addBarrageAndUser(HttpServletRequest request, HttpServletResponse response, BarrageAndUser barrageAndUser) {
		JSONObject json = new JSONObject();
		int number = barrageAndUserService.saveBarrageAndUser(barrageAndUser);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 修改弹幕和在线用户
	 */
	@RequestMapping("/alertBarrageAndUser")
	public ModelAndView alertBarrageAndUser(HttpServletRequest request, HttpServletResponse response,int id) {
		ModelAndView mod = new ModelAndView("/barrageAndUser/alertBarrageAndUser");
		BarrageAndUser obj = barrageAndUserService.findBarrageAndUserOne(id);
		mod.addObject("obj",obj);
		return mod;
	}
	
	/**
	 * 保存修改的弹幕和在线用户
	 */
	@ResponseBody
	@RequestMapping("alertBarrageAndUserSave")
	public JSONObject alertBarrageAndUserSave(HttpServletRequest request, HttpServletResponse response, BarrageAndUser barrageAndUser) {
		JSONObject json = new JSONObject();
		int number = barrageAndUserService.alertBarrageAndUserSave(barrageAndUser);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	
	/**
	 * 删除弹幕和在线用户
	 */
	@ResponseBody
	@RequestMapping("deleteBarrageAndUser")
	public JSONObject deleteBarrageAndUser(HttpServletRequest request, HttpServletResponse response, int id) {
		JSONObject json = new JSONObject();
		int number = barrageAndUserService.deleteBarrageAndUser(id);
		if (number == 1) {
			json.put("missage", "success");
		} else {
			json.put("missage", "error");
		}
		return json;
	}
	/**
	 * 新增用户头像上传
	 * @param response
	 * @param uploadFile
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/uploadClassifyImg")
	@ResponseBody
	public JSONObject uploadClassifyImg(HttpServletResponse response,@RequestParam("file_data")Part uploadFile) throws IOException{
		String imgUrl = null;
		try{
			imgUrl = barrageAndUserService.uploadClassifyImg2WSCDN(uploadFile);
		}catch(Exception e){
			return UploadRes.error("文件上传失败");
		}
		JSONObject result = UploadRes.success()
				.setFileUrl(imgUrl)
				.setFileType(FileType.IMAGE)
				.setDelelteUrl("deletefile")
				.putExtraParam("path", imgUrl).toJSON();
		return result;
	}
}
